package com.huayal.mall.ware.service.impl;

import com.huayal.common.utils.R;
import com.huayal.common.vo.SkuInfoEntityDTO;
import com.huayal.common.vo.es.SkuHasStockDTO;
import com.huayal.mall.ware.fegin.ProductFeginService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huayal.common.utils.PageUtils;
import com.huayal.common.utils.Query;

import com.huayal.mall.ware.dao.WareSkuDao;
import com.huayal.mall.ware.entity.WareSkuEntity;
import com.huayal.mall.ware.service.WareSkuService;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Resource
    WareSkuDao wareSkuDao;

    @Autowired
    ProductFeginService productFeginService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String)params.get("skuId");
        if (StringUtils.isNotEmpty(skuId)){
            wrapper.eq("sku_id",skuId);
        }

        String wareId = (String)params.get("wareId");
        if (StringUtils.isNotEmpty(wareId)){
            wrapper.eq("ware_id",wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //判断是否有该商品和仓库的入库记录
        List<WareSkuEntity> wareSkuEntities = this.baseMapper.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (wareSkuEntities==null||wareSkuEntities.size()==0){
            //没有，新增商品库存记录
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            //需要动态的设置商品的名称
            try {
                R info = productFeginService.info(skuId);
                if (info.getCode()==0){
                    Map skuInfo =(Map<String,Object>)info.get("skuInfo");
                    wareSkuEntity.setSkuName(skuInfo.get("skuName")+"");
                }
            }catch (Exception e){

            }

            this.save(wareSkuEntity);
        }else {
            //有，更新库存
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }



    }

    /**
     * 批量判断是否有库存
     * @param skuIds
     * @return
     */
    @Override
    public List<SkuHasStockDTO> skuHasStock(List<Long> skuIds) {

        List<SkuHasStockDTO> list = skuIds.stream().map(skuId -> {
            SkuHasStockDTO skuHasStockDTO = new SkuHasStockDTO();

            Integer sum = wareSkuDao.skuHasStock(skuId);
            skuHasStockDTO.setSkuId(skuId);
            skuHasStockDTO.setHasStock(sum!=null&&sum>0?true:false);

            return skuHasStockDTO;
        }).collect(Collectors.toList());

        return list;
    }

}